<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>函数节流</title>
    <style>
        #box{
            width: 200px;
            height: 200px;
            position: absolute;
            top: 100px;
            left: 100px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <button id="star">开始</button>
    <script>
        var box=document.getElementById('box');
        var star=document.getElementById('star');
        var lock=true;
        var i=1;
        
        star.onclick=function(){
            box.style.transition='all 2s linear 0s'
            if(!lock) return;
            if(i==1){
            box.style.left='1100px'
            i=2;
           }else if(i==2){
            box.style.left='100px'
            i=1;
           }
           lock=false;
           setTimeout(function(){
            lock=true;
           },2000)
        }
   

    </script>
</body>
</html>